﻿using Lierda.WPFHelper;
using System;
using System.Threading;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using static System.Windows.Forms.Menu;
using YanShu.Utils;

namespace YanShu
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : System.Windows.Application
    {
        //实例化
        public static App current;
        //托盘
        NotifyIcon notifyIcon = null;
        //回收内存
        LierdaCracker cracker = new LierdaCracker();

        Mutex mutex;
        public App()
        {
            this.Startup += new StartupEventHandler(App_Startup);
            current = this;
            InitialTray();
            cracker.Cracker(30);
            ConnectToServer();
        }
        /// <summary>
        /// 连接服务器
        /// </summary>
        public void ConnectToServer()
        {
            Client.Instance.Start();
        }
        void App_Startup(object sender, StartupEventArgs e)
        {
            bool ret;
            //下面的RemoteDesktop改成自己项目名字或喜欢的一个标识
            mutex = new Mutex(true, "XiaoYanShu", out ret);
            if (!ret)
            {
                App.ShowBalloonTip("注意", "小鼹鼠已在挖洞！！！");
                notifyIcon.Dispose();
                current.Shutdown();
            }
        } 
        private void InitialTray()
        {
            //隐藏主窗体
            // this.Visibility = Visibility.Hidden;
            //设置托盘的各个属性
            notifyIcon = new NotifyIcon();
            //汽包提示球
            //notifyIcon.BalloonTipText = "程序开始运行！！！";
            //notifyIcon.ShowBalloonTip(5000);
            notifyIcon.Text = "内网穿透";
            //设置托盘图标
            Uri uri = new Uri("pack://application:,,,/Resources/stray.ico");
            notifyIcon.Icon = new System.Drawing.Icon(GetResourceStream(uri).Stream);
            notifyIcon.Visible = true;

            notifyIcon.MouseClick += new MouseEventHandler(NotifyIcon_MouseClick);

            /* //设置菜单项
             MenuItem menu1 = new MenuItem("菜单一");
             //菜单 点击事件
             menu1.Click += new EventHandler(Menu1_Click);
             MenuItem menu2 = new MenuItem("菜单二");

             MenuItem menu = new MenuItem("菜单", new MenuItem[] { menu1, menu2 });*/

            //退出菜单项
            MenuItem exit = new MenuItem("退出");

            //菜单 点击事件
            exit.Click += new EventHandler(Exit_Click);

            //关联托盘控件
            MenuItem[] childen = new MenuItem[] { exit };
            notifyIcon.ContextMenu = new ContextMenu(childen);

        }

        public void addMenus()
        {
            MenuItemCollection contextMenu = current.notifyIcon.ContextMenu.MenuItems;
            //退出菜单项
            MenuItem exit = new MenuItem("退出");
            //菜单 点击事件
            exit.Click += new EventHandler(Exit_Click);
            //退出菜单项
            MenuItem about = new MenuItem("关于");
            //菜单 点击事件
            about.Click += About_Click;
            //退出菜单项
            MenuItem logout = new MenuItem("注销");
            //菜单 点击事件
            logout.Click += Logout_Click;
            contextMenu.Clear();
            contextMenu.Add(logout);
            contextMenu.Add(about);
            contextMenu.Add(exit);

        }

        private void Logout_Click(object sender, EventArgs e)
        {
            //清空配置文件
            CommonUtils.WriteConfig("");
            //切换到登录页面
            YanShu.MainWindow.SwtichToLogin();

            Login.HideLoading();
            Login.current.account.Text = "";
            Login.current.password.Password = "";

            MenuItemCollection contextMenu = current.notifyIcon.ContextMenu.MenuItems;

            //退出菜单项
            MenuItem exit = new MenuItem("退出");

            //菜单 点击事件
            exit.Click += new EventHandler(Exit_Click);

            contextMenu.Clear();

            contextMenu.Add(exit);
        }

        private void About_Click(object sender, EventArgs e)
        {
            ShowBalloonTip("关于","小鼹鼠-内网穿透");
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            AppShutdown();
        }
        private void NotifyIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                MainWindow.WindowState = WindowState.Normal;
                MainWindow.Show();
            }
        }

        public static void ShowBalloonTip(String tipTitle, String tipText)
        {
            try
            {
                current.notifyIcon.ShowBalloonTip(5, tipTitle, tipText, ToolTipIcon.None);
            }
            catch (Exception e)
            {
                String message = e.Message;
            }
        }

        public static void AppShutdown()
        {
            current.notifyIcon.Dispose();
            Current.Shutdown();
        }
    }
}
